/*函数节流*/
export const throttle = (func, wait = 100, options = null) => {
  let previous = 0;
  let timerid = null;

  if (!options) {
    options = {
      leading: true,
    };
  }

  return function (...args) {
    const now = Date.now();

    if (!previous && !options.leading) previous = now;

    const remaining = wait - (now - previous);
    const context = this;

    if (remaining <= 0) {
      if (timerid) {
        clearTimeout(timerid);
        timerid = null;
      }
      previous = now;
      func.apply(context, args);
    }
  };
};


/*函数防抖*/
export const debounce = function (func, wait = 500) {
  let timerId;
  return function (...rest) {
    if (timerId) {
      clearTimeout(timerId);
    }
    timerId = setTimeout(() => {
      func.apply(this, rest);
    }, wait);
  };
};

/**
 * 获取界面上的节点信息
 * @param {*} context  上下文的this
 * @param {*} selector 元素
 * @param {*} needAll 
 */
export const getRect = function (context, selector, needAll = false) {
  return new Promise((resolve, reject) => {
    context
      .createSelectorQuery()
    [needAll ? 'selectAll' : 'select'](selector)
      .boundingClientRect((rect) => {
        if (rect) {
          resolve(rect);
        } else {
          reject(rect);
        }
      })
      .exec();
  });
};

/**
 * 判断参数是否为空
 * @param {*} a - 需要检查的值
 */
export const isEmpty = (a) => {
  if (typeof a === 'boolean') return false; // 新增：明确布尔值不为空

  if (a === "") return true; // 检验空字符串
  if (a === "null") return true; // 检验字符串类型的null
  if (a === "undefined") return true; // 检验字符串类型的 undefined
  if (!a && a !== 0 && a !== "") return true; // 检验 undefined 和 null           
  if (Array.isArray(a) && a.length === 0) return true; // 检验空数组（优化判断方式）
  if (a instanceof Object && Object.keys(a).length === 0) return true;  // 检验空对象
  return false;
};

/**
 * 根据key获取文本
 * @param {*} key 
 * @param {Object} value key所在的对象
 */
export const keyToLabel = (key, value) => {
  let resStr = key.split(".").reduce((init, item) => {
    init = init[item]
    return init;
  }, value);
  return resStr;
}

/**
 * 二次确认
 */
export const submitConfirm = () => {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '温馨提示',
      content: '是否提交',
      confirmText: "是",
      cancelText: "否",
      confirmColor: "#2577E3",
      complete: (res) => {
        if (res.confirm) {
          resolve()
        }
        if (res.cancel) {
          reject()
        }
      }
    })
  })

}

/**
 * dicListToMap
 */
export const dicListToMap = (list = []) => {
  return list.reduce((init, item) => {
    init[item.value] = item.label
    return init
  }, {})

}


export function checkParams(params) {
  if (typeof params != 'object') return params
  for (let key in params) {
    const value = params[key];
    if (value === null || value === undefined || value === "") {
      delete params[key];
    }
  }
  return params;
}



/**
 * 判断是否有code
 */
export const pathToParams = (path) => {
  let pathParams = path
    ?.split("?")[1]
    ?.split("&")
    .reduce((init, item) => {
      let [key, value] = item.split("=");
      init[key] = value;
      return init;
    }, {});
  return pathParams;
}